﻿using System;
using System.Web;
using System.Web.Mvc.Html;
using raya.Library.Helpers;

namespace raya.Library.Extensions
{
    public enum rayaControls
    {
        activities,
        applications,
        apps_active,
        apps_fav,
        apps_gallery,
        apps_manage,
        c_contacts,
        c_login,
        c_members_recent,
        c_photos,
        c_register,
        c_tagcloud,
        friends,
        gadget,
        inventory,
        notify,
        notifications,
        notifications_friend,
        profile_edit,
        profile_edit_no,
        profile_info
    }

    public enum ScriptLibrary
    {
        All,
        Default
    }

    public static class ViewExtensions
    {
        public static void RenderControl(this System.Web.Mvc.HtmlHelper helper,
                                         rayaControls control, object data)
        {
            //data = data ?? new object();
            string controlName = Enum.GetName(typeof(rayaControls), control);
            helper.RenderPartial(controlName, data, helper.ViewContext.ViewData);
        }

        /// <summary>
        /// A helper for registering script tags on an MVC View page.
        /// </summary>
        public static string RegisterJS(this System.Web.Mvc.HtmlHelper helper, ScriptLibrary scriptLib)
        {
            //get the directory where the scripts are
            string scriptRoot = VirtualPathUtility.ToAbsolute("~/Scripts");
            const string scriptFormat = "<script src=\"{0}/{1}\" type=\"text/javascript\"></script>\r\n";

            string scriptLibFile;
            string result;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            //all of the script tags
            switch (scriptLib)
            {
                case ScriptLibrary.All:
                    string[] scripts = Enum.GetNames(typeof(ScriptLibrary));

                    foreach (string script in scripts)
                    {
                        scriptLibFile = script + ".js";
                        sb.AppendFormat(scriptFormat, scriptRoot, scriptLibFile);
                    }
                    result = sb.ToString();
                    break;
                case ScriptLibrary.Default:
                    sb.AppendFormat(scriptFormat, scriptRoot, "jquery-ui.js");
                    sb.AppendFormat(scriptFormat, scriptRoot, "general.js");
                    result = sb.ToString();
                    break;

                default:
                    //just the ones asked for
                    scriptLibFile = Enum.GetName(typeof(ScriptLibrary), scriptLib) + ".js";
                    result = string.Format(scriptFormat, scriptRoot, scriptLibFile);
                    break;
            }
            
            return result;
        }
        public static string RegisterJS(this System.Web.Mvc.HtmlHelper helper, string script, bool addScriptTags)
        {
            string result;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(script);
            if (addScriptTags)
            {
                result = "<script type=\"text/javascript\">" + sb + "</script>";
            }
            else
            {
                result = sb.ToString();
            }

            return result;
        }
    }
}